import instance from '../http';

// 过滤条件怎么加
export const getArticles = (page, size, is_deleted = "", is_private = "", categories=[], ordering=null,keywords="",searchType="title") => {
    let url = "/articles/?page=" + page + "&size=" + size
    if (is_deleted == "true" || is_deleted == "false")
        url += "&is_deleted=" + is_deleted
    if (is_private == "true" || is_private == "false")
        url += "&is_private=" + is_private
    if(categories.length!=0){
        for(let c of categories){
            url += "&category="+c
        }
    }
    if(ordering){
        url+="&ordering="+ordering
    }

    if(keywords!=""){
        url+="&"+searchType+"_keywords="+keywords
    }


    return instance({
        url,
        method: "get"
    })
}


export const createArticle = (data) => {
    return instance({
        url: "/articles/",
        method: "post",
        data: data
    })
}


export const updateArticle=(id,data)=>{
    return instance({
        url: "/articles/" + id+"/",
        method:"put",
        data
    })
}

export const tagArticle = (id, tags = [], new_tags = []) => {
    return instance({
        url: "/articles/" + id + "/add_tags/",
        method: "post",
        data: { tags, new_tags }

    })
}

export const deleteArticle = (id) => {
    return instance({
        url: "/articles/" + id + "/",
        method: "delete",

    })
}

export const restoreArticle = (id) => {
    return instance({
        url: "/articles/" + id + "/restore/",
        method: "post",

    })
}

export const bulkToggleArticleDelete = (ids) => {
    return instance({
        url: "/articles/bulk_toggle_delete/",
        method: "post",
        data: ids
    })
}

export const getArticle = (id)=>{
    return instance({
        url:"/articles/"+id+"/",
        method:"get"
    })
}